

clear

	* Identify model with best fit
		forvalues v =1(1)5 {
			qui: insheet using "${data}v11_Parameters_`v'.csv"
			qui: destring v7, replace force
			qui: destring v8, replace force

			qui: su v7 if _n ==1
			qui: global lv`v' : di %4.1f `r(mean)'
			qui: su v8 if _n ==1
			qui: global flag`v' = `r(mean)'

			qui: clear
		}
		qui:set obs 5
		qui:ge loss =.
		qui:ge flag =.

		qui:ge sim = _n
		forvalues v =1(1)5 {
		qui: replace loss = ${lv`v'} if sim == `v'
		qui: replace flag = ${flag`v'} if sim == `v'
		}
		gsort loss sim
		su sim if _n ==1
		global best1 =    `r(mean)'


		clear
		
		* Table with all parameters
		forvalues v =1(1)5 {
			qui: insheet using "${data}v11_Parameters_`v'.csv"
			keep v1 
			drop if _n >7
			destring v1, replace force
			rename v1 sim`v'
			ge id =_n
			tempfile parameters`v' 
			save `parameters`v'' 
			clear
			
			insheet using "${data}v11_Parameters_`v'.csv"
			keep v2 
			drop if _n >11
			rename v2 sim`v'
			ge id =_n
			tempfile moments`v' 
			save `moments`v'' 
			clear
		}
		
		
		use `parameters1'
		forvalues v =2(1)5 {
			merge 1:1 id using `parameters`v'' 
			drop _merge
		}
		
		save "${data}v11_allparameters.dta", replace
		clear
		
		use `moments1'
		forvalues v =2(1)5 {
			merge 1:1 id using `moments`v'' 
			drop _merge
		}
		save "${data}v11_allmoments.dta", replace
		clear
		
		
		* Moment fit
		insheet using "${data}v11_Moments_all_${best1}.csv"
		
	
	
		ge moment = _n
		drop if moment >16 

		ge m= 1 if moment <5
	replace m = 2 if moment >4 & moment <9 
	replace m = 3 if moment >8  &moment <13
	replace m = 4 if moment >12 



	su v1 if _n ==1
	global a1_a = `r(mean)'

	su v1 if _n ==2
	global a1_b = `r(mean)'

	su v1 if _n ==3
	global a1_c = `r(mean)'

	su v1 if _n ==4
	global a1_d = `r(mean)'

	su v1 if _n ==5
	global a2_a = `r(mean)'

	su v1 if _n ==6
	global a2_b = `r(mean)'

	su v1 if _n ==7
	global a2_c = `r(mean)'

	su v1 if _n ==8
	global a2_d = `r(mean)'

	su v1 if _n ==9
	global x_a = `r(mean)'

	su v1 if _n ==10
	global x_b = `r(mean)'

	su v1 if _n ==11
	global x_c = `r(mean)'

	su v1 if _n ==12
	global x_d = `r(mean)'

	su v1 if _n ==13
	global s_a = `r(mean)'

	su v1 if _n ==14
	global s_b = `r(mean)'

	su v1 if _n ==15
	global s_c = `r(mean)'



	clear


	insheet using "${data}v11_Parameters_${best1}.csv"
	

	destring v2, replace force
	su v2 if _n ==3
	local a2_c = `r(mean)'
	su v2 if _n ==4
	local a2_d = `r(mean)'
	keep v1 
	drop if _n >7

	destring v1, replace force
	
	

	forvalues v = 1(1)7{
		su v1 if _n == `v'
		local r`v' : di %5.4f `r(mean)'
	}
	display "r5 " `r5'
	display "r7 " `r7'
	display "r1 " `r1'
	display "r2 " `r2'
	


	local r8 : di %4.2f (`r2'+ (`r1' * `a2_c')) / (`r2') -1
	local r9 : di %4.2f (`r2'+ (`r1' * `a2_d')) / (`r2') -1
	local r10 : di %4.3f  (`r3') * ((${a1_d})^2)
	local r11 : di %4.3f  (`r4') * ((${a2_d})^2)
	local r8  = `r8'*100
	local r9 = `r9'*100
	display `r8'
	display ${a2_c}
	display ${a2_d}



	#delimit ;
	file open myfile using "${output}TableA11.tex", write replace;
	file write myfile   _n "\begin{tabular}{lc}"			
	_n "\thickhline"
	_n "& \multicolumn{1}{c}{(1)\mbox{\ }}   \\"
	_n "\hline"		
	_n "\mbox{}\\"
	_n " Worker return to effort \$\alpha\$ & `r2'    \\ "	
	_n " Supervisor return to effort  \$\beta\$ & `r1'    \\ "	
	_n " Worker baseline incentive \$b_1\$  & `r5'     \\ "	
	_n " Supervisor baseline incentive  \$b_2\$ 	    &`r6'  \\	"
	_n " \mbox{}\\"
	_n " Calibrated friction \$z\$ & 3.6    \\ "	
	_n " \mbox{}\\"
	_n "\thickhline"
	_n "\end{tabular}" ;
	file close myfile;
	#delimit cr
	

	clear

	insheet using "${data}v11_Moments_all_${best1}.csv"
	destring v1, replace force
	su v1 if _n ==13
	global r13_c2  : di %4.2f `r(mean)'

	su v1 if _n ==14
	global r14_c2 : di %4.3f `r(mean)'

	su v1 if _n ==15
	global r15_c2 : di %4.3f `r(mean)'
	

	
	clear
	
	insheet using "${data}v11_Parameters_${best1}.csv"
	
	
	
	
	destring v7, replace force
	su v7 if _n ==1
	local loss1 : di %4.1f `r(mean)'

	keep v2 v3
	drop if _n >12
	rename v2 simulated
	rename v3 real
	destring simulated, replace force
	destring real, replace force
	forvalues v = 1(1)12{
	su simulated if _n == `v'
	local r`v'_c2 : di %4.3f  `r(mean)'
		su real if _n == `v'
	local r`v'_c1 : di %4.3f `r(mean)'
	}



	
	#delimit ;	
	file open myfile using "${output}TableA12.tex", write replace;
	file write myfile   _n "\begin{tabular}{lcc}"			
	_n "\thickhline"
	_n "Moments &  Targeted Real & Simulated    \\"
	_n "\hline"		
	_n "\mbox{}\\"
	_n " Supervisor effort in worker incentives group & `r1_c1' & `r1_c2'    \\ "	
	_n " Supervisor effort in supervisor incentives group		    &`r2_c1'  & `r2_c2'   \\ "
	_n " Supervisor effort in shared incentives group  & `r3_c1'  & `r3_c2'   \\ "	
	_n " Supervisor effort in control group   &`r4_c1' & `r4_c2' \\	"
	_n " Output in worker incentives group & `r5_c1'  & `r5_c2'   \\ "	
	_n " Output in supervisor incentives group 	    &`r6_c1' & `r6_c2' \\	"
	_n " Output in shared incentives group & `r7_c1'  & `r7_c2'   \\ "	
	_n " Output in control group 	    &`r8_c1' & `r8_c2' \\	"
	_n " \mbox{}\\"
	_n " \hline"
	_n " Value loss function 	    & \multicolumn{2}{c}{`loss1'} \\	"
	_n "\thickhline"
	_n "\end{tabular}" 
	_n " \mbox{}\\" 
	_n "\footnotesize"
	_n " Notes: The table shows the targeted empirical moments used for minimum distance estimation as well as the simulated moments.   ";
	file close myfile;
	#delimit cr
	clear




